﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows;
using System.Diagnostics;
using System.ComponentModel;
using System.Reflection;

namespace ISRecord.View.Services
{
    [DebuggerNonUserCode()]
    public class DelegateCommand : ICommand
    {

        Action<object> execute;
        Predicate<object> canExecute;
        string commandName;

        public string Text
        {
            get { return commandName; }
        }


        public DelegateCommand(Action<object> execute, Predicate<object> canExecute, string commandName)
        {
            if (execute == null)
            {
                throw new ArgumentNullException("execute");
            }
            this.execute = execute;
            this.canExecute = canExecute;
            this.commandName = commandName;
        }
        public DelegateCommand(Action<object> execute)
            : this(execute, null, string.Empty)
        {

        }

        public bool CanExecute(object parameter)
        {
            return canExecute == null ? true : canExecute(parameter);
        }


        public event EventHandler CanExecuteChanged
        {
            add
            {
                CommandManager.RequerySuggested += value;

            }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public void Execute(object parameter)
        {
            execute(parameter);
        }

        public override string ToString()
        {
            return commandName;
        }

        public void RemoveHandler()
        {
            this.execute = null;
            this.canExecute = null;
            commandName = null;
        }

    }


}
